home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3184 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  95 lines

  1. Path: news.umbc.edu!not-for-mail
  2. From: schlein@umbc.edu (Jonas J. Schlein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: sscanf problems
  5. Date: 26 Jan 1996 14:19:30 -0500
  6. Organization: University of Maryland Baltimore County
  7. Message-ID: <4eb9g2$8sa@umbc9.umbc.edu>
  8. References: <4e4c2v$j2g@mathserv.mps.ohio-state.edu>
  9. NNTP-Posting-Host: umbc9.umbc.edu
  10. NNTP-Posting-User: schlein
  11.  
  12. Chris Mongold  <cmongold@magnus.acs.ohio-state.edu> wrote:
  13. |> I'm sorry if this is an inappropriate topic,
  14.  
  15. The topic is appropriate, but the problems are not.
  16.  
  17. |> but I've tried everything else.
  18.  
  19. If reading the FAQ is included in "everything" then that you have not done.
  20.  
  21. |> I can't seem to get sscanf to to separate a string into various variable
  22. |> types.  Here is an example:
  23. |> 
  24. |> #include <stdio.h>
  25. |> 
  26. |> void main()
  27.  
  28. What's that? You definitely want 'int main (void)' instead.
  29.  
  30. |> {
  31. |> char input[20], crap[17], segment[6], seg_len[3], begin[3], load[3];
  32. |> int type;
  33. |> FILE *fp;
  34. |> 
  35. |> printf("File: ");
  36. |> gets(input);
  37.  
  38. Wonder what happens when the user gets sloppy and types 21 characters?
  39.  
  40. |> fp = fopen(input, "r");
  41.  
  42. Are you sure it's really open?
  43.  
  44. |> while(!feof(fp))
  45.  
  46. This is not a Pascal class ;-). The feof() function is most likely not
  47. what you want because it answers the question "Have I already read past
  48. the end of file?", opposed to "Will the next thing I read take me past
  49. the end of file?"...
  50.  
  51. |> {
  52. |> fgets(crap, 17, fp);
  53. |> sscanf(crap, "%d%3s%6s%3s%3s", type, begin, segment, seg_len, load);
  54.  
  55. Well 'type' is an int so you need an & before it.
  56.  
  57. |> printf("%d %3s %6s %3s %3s", type, begin, segment, seg_len, load);
  58. |> 
  59. |> }
  60.  
  61. And add:
  62.  
  63. return (0);
  64.  
  65. |> }
  66. |> No matter what I do, it always 'bus errors' when I sscanf.
  67.  
  68. Not surprising at all...Since the undefined value of 'type' is probably
  69. not a good memory address such an error is to be expected.
  70.  
  71. |> I've tried it several different ways, including making crap larger, but
  72. |> to no avail.
  73.  
  74. That's not it at all since you use fgets() 'crap' is guaranteed not to
  75. overflow.
  76.  
  77. |> If anyone can help me, I would greatly appreciate it.
  78.  
  79. Well here's some stuff that will help...Read the following sections
  80. of the comp.lang.c FAQ which is freely available from rtfm.mit.edu:
  81.  
  82. void main() related: 11.12, 11.14, 11.15
  83. gets() related:       7.1,  12.23
  84. [fs]scanf() related: 12.12
  85. feof() related:      12.2
  86.  
  87. I believe these FAQ questions and answers will answer all your problems
  88. for this particular program. Please consult it first to see if your
  89. question has already become [in]famous enough to make it into this
  90. collection.
  91. -- 
  92. "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
  93.  
  94. Jonas J. Schlein  (schlein@gl.umbc.edu)
  95.